package com.xxjz.sc.controller.user;

import com.xxjz.sc.bean.user.User;
import com.xxjz.sc.mapper.user.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * jdbcTemplate方式
     * @return
     */
    @ResponseBody
    @RequestMapping("/userall")
    public String getAllUser(){
        String sql = "select * from user";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        for (Map<String, Object> m : maps){
            System.out.println(m.toString());
        }
        return maps.toString();
    }

    @Autowired
    UserMapper userMapper;

    /**
     * 注解方式
     * @return
     */
    @GetMapping("/userall2")
    public String getAllUser2(){
        List<User> allUser = userMapper.getAllUser();
        for (User u : allUser){
            System.out.println(u.toString());
        }
        return "";
    }

    /**
     * 注解方式
     * @return
     */
    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable("id") Integer id){
        return userMapper.getUserById(id);
    }

    /**
     * 注解方式
     * @return
     */
    @GetMapping("/user")
    public User insertUser(User user){
        userMapper.insertUser(user);
        return user;
    }

}
